package com.ruoyi.source.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.source.api.domain.SrcCity;
import com.ruoyi.source.api.domain.SrcProduct;
import com.ruoyi.source.service.ISrcProductService;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 产品信息Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/product")
public class SrcProductController extends BaseController {
    @Autowired
    private ISrcProductService srcProductService;

    /**
     * 查询产品信息列表
     */
    @RequiresPermissions("source:product:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcProduct srcProduct)
    {
        startPage();
        List<SrcProduct> list = srcProductService.selectSrcProductList(srcProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @RequiresPermissions("source:product:export")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcProduct srcProduct)
    {
        List<SrcProduct> list = srcProductService.selectSrcProductList(srcProduct);
        ExcelUtil<SrcProduct> util = new ExcelUtil<SrcProduct>(SrcProduct.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @RequiresPermissions("source:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(srcProductService.getById(productId));
    }

    /**
     * 新增产品信息
     */
    @RequiresPermissions("source:product:add")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcProduct srcProduct)
    {
        return toAjax(srcProductService.insertSrcProduct(srcProduct));
    }

    /**
     * 修改产品信息
     */
    @RequiresPermissions("source:product:edit")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcProduct srcProduct)
    {
        srcProduct.setUpdateBy(SecurityUtils.getUsername());
        srcProduct.setUpdateTime(DateUtils.getNowDate());
        return toAjax(srcProductService.updateById(srcProduct));
    }

    /**
     * 删除产品信息
     */
    @RequiresPermissions("source:product:remove")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(srcProductService.removeBatchByIds(Arrays.asList(productIds)));
    }

    /**
     * 获取产品存储方式下拉列表
     */
    @GetMapping("/typeList")
    public AjaxResult typeList(){
        List<SysDictData> dataList=srcProductService.typeList();
        return AjaxResult.success(dataList);
    }


    @GetMapping("/selectTreePlaceList")
    public List<SrcCity> treeplace() {
        List<SrcCity> places = srcProductService.listcity();
        ArrayList<SrcCity> placeList = new ArrayList<>();
        List<SrcCity> collect = places.stream().filter(pp -> pp.getParentId() == 1).collect(Collectors.toList());
        for (SrcCity srccity : collect) {
            placeList.add(fifter(places,srccity));
        }
        return placeList;
    }

    private SrcCity fifter(List<SrcCity> places, SrcCity srcCity) {
        ArrayList<SrcCity> placesList = new ArrayList<>();
        for (SrcCity pp : places) {
            if(pp.getParentId()!=null && pp.getParentId().equals(srcCity.getCityId())){
                placesList.add(fifter(places,pp));
            }
            if(!placesList.isEmpty()){
                srcCity.setChildren(placesList);

            }
        }
        return srcCity;
    }

}
